package zhieasy.cn.service.common.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import zhieasy.cn.common.utils.CmdScriptUtils;
import zhieasy.cn.common.utils.FileUtils;
import zhieasy.cn.common.utils.SystemTypeUtils;
import zhieasy.cn.service.common.CommonInitService;
import zhieasy.cn.service.common.script.AuthCodeSession;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 公用服务初始化
 * @author zouchanglin
 */
@Slf4j
@Service
public class CommonInitServiceImpl implements CommonInitService {
    public static final Map<String, AuthCodeSession> weChatSessionCache = new ConcurrentHashMap<>();

    public static final File verificationScript = new File("/opt/proj/xpu-helper/script/GetVerificationCode.py");
    public static final File userInfoScript = new File("/opt/proj/xpu-helper/script/GetUserInfo.py");
    public static final File courseInfoScript = new File("/opt/proj/xpu-helper/script/GetCourseInfo.py");

    public static final File verificationScriptWin = new File("C:\\Users\\15291\\Desktop\\script\\GetVerificationCode.py");
    public static final File userInfoScriptWin = new File("C:\\Users\\15291\\Desktop\\script\\GetUserInfo.py");
    public static final File courseInfoScriptWin = new File("C:\\Users\\15291\\Desktop\\script\\GetCourseInfo.py");

    @PostConstruct
    public void init(){
        ClassPathResource verificationPath = new ClassPathResource("script/GetVerificationCode.py");
        ClassPathResource userInfoPath = new ClassPathResource("script/GetUserInfo.py");
        ClassPathResource courseInfoPath = new ClassPathResource("script/GetCourseInfo.py");
        try {
            // Debug for Windows
            if(SystemTypeUtils.isWindows()){
                FileUtils.createFiles(new File[]{verificationScriptWin, userInfoScriptWin, courseInfoScriptWin});
                FileUtils.copyInputStreamToFile(verificationPath.getInputStream(), verificationScriptWin);
                FileUtils.copyInputStreamToFile(userInfoPath.getInputStream(), userInfoScriptWin);
                FileUtils.copyInputStreamToFile(courseInfoPath.getInputStream(), courseInfoScriptWin);
            }else{
                FileUtils.createFiles(new File[]{verificationScript, userInfoScript, courseInfoScript});
                FileUtils.copyInputStreamToFile(verificationPath.getInputStream(), verificationScript);
                FileUtils.copyInputStreamToFile(userInfoPath.getInputStream(), userInfoScript);
                FileUtils.copyInputStreamToFile(courseInfoPath.getInputStream(), courseInfoScript);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public String getAuthCodeImage(String openId) {
        try {
            String tmpDir = System.getProperty("java.io.tmpdir");
            String cmd;
            if(SystemTypeUtils.isWindows()){
                cmd = "python " + verificationScriptWin.getAbsolutePath() + " " + tmpDir;
            }else{
                cmd = "python " + verificationScript.getAbsolutePath() + " " + tmpDir;
            }
            String result = CmdScriptUtils.execCmd(cmd, null);
            log.info("GetAuthCodeImage result={}", result);
            AuthCodeSession authFirstData = JSON.parseObject(result, AuthCodeSession.class);
            if("0".equals(authFirstData.getError())) {
                weChatSessionCache.put(openId, authFirstData);
                String authCodeImgPath = authFirstData.getData().getFile_address();
                String name = new File(authCodeImgPath).getName();
                log.info("AuthCodeImgName ={}", name);
                return name;
            }else{
                log.error("Error! Call python & system error!");
            }
        } catch (Exception e) {
            log.error("CommonInitServiceImpl error!", e);
        }
        return "";
    }
}
